//
//  StoreCell.m
//  BosidengIphone
//
//  Created by  on 11-12-31.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "StoreCell.h"
#import "AppDelegate.h"
#import "MapViewController.h"
#import "Place.h"

@implementation StoreCell
@synthesize title;
@synthesize address;
@synthesize zipcode;
@synthesize phone;
@synthesize businessHours;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)dealloc {
    [title release];
    [address release];
    [zipcode release];
    [phone release];
    [businessHours release];
    [super dealloc];
}

- (NSString *)cleanPhoneNumber:(NSString *)number {
    NSString *str = [NSString stringWithString:number];
    return [[[str stringByReplacingOccurrencesOfString:@" " withString:@""]
             //stringByReplacingOccurrencesOfString:@"-" withString:@""]
             stringByReplacingOccurrencesOfString:@"(" withString:@""]
             stringByReplacingOccurrencesOfString:@")" withString:@""];
}

- (IBAction)callPhone:(id)sender {
    NSURL *phoneUrl = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", [self cleanPhoneNumber:mData.phone]]];
    NSLog(@"make call, url=%@", phoneUrl);
    [[UIApplication sharedApplication] openURL:phoneUrl];
}

- (IBAction)openMap:(id)sender {
    Place *p = [[Place alloc] initWithLatitude:[mData.latitude floatValue] andlongitude:[mData.longitude floatValue] andTitle:mData.name andDetail:nil];
    NSArray *places = [NSArray arrayWithObject:p];
    [p release];
    [mViewController jumpToMapView:places];
}

- (void)config:(id)viewController data:(StoreData *)data {
    mViewController = viewController;
    mData = data;
    
    title.text = mData.name;
    address.text = [NSString stringWithFormat:@"%@%@", 
                    [AppDelegate getLanguageString:@"store_address"],
                    mData.address];
    zipcode.text = [NSString stringWithFormat:@"%@%@", 
                    [AppDelegate getLanguageString:@"store_zipcode"],
                    mData.zipCode];
    businessHours.text = [NSString stringWithFormat:@"%@%@", 
                    [AppDelegate getLanguageString:@"store_business_hours"],
                    mData.businessHours];
    phone.text = [NSString stringWithFormat:@"%@%@", 
                          [AppDelegate getLanguageString:@"store_phone"],
                          mData.phone];
}

@end
